const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ProductList:[],
    loading:true,
    isempty:false,
    currPage:1,
    perPage:10,
    ProductsList:[],
    time:0,
    NoMore:false,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 修改主题
    wx.setNavigationBarTitle({
      title: options.keyword
    })
    this.getProductsList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that=this;
    that.data.currPage=1;
    that.setData({
      NoMore:false
    })
    that.getProductsList();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 2000);
  },
  onPageScroll: function(e) {
    let that=this;
    if (e.scrollTop > 200) {
      that.selectComponent("#totop").open();
    } else {
      that.selectComponent("#totop").close();
    }
    // 超出停止视频
    const videoId = app.videoId;
    if(videoId){
      let query = wx.createSelectorQuery();
      let wh = wx.getSystemInfoSync().windowHeight;
      query.select('#productItem' + videoId).boundingClientRect(rect => {
            if (rect.top < -rect.height || rect.bottom > (rect.height+wh)) {
              app.videoId = null;
              app.videoContext[videoId].pause();
            }
      }).exec();
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that=this;
    if(that.data.NoMore==true)return;
    that.setData({
      loading:true
    })
    that.data.currPage = parseInt(that.data.currPage) + 1;
    that.getProductsList();
  },

  getProductsList() {
    let that = this;
    that.setData({
      loading:true
    })
    setTimeout(()=>{
      that.setData({
        ProductList:that.data.ProductList.concat(app.data.productList),
        loading:false
      })
    },2000)
    // let user = app.functions.getLocalStorage("user");
    // let shop = app.functions.getLocalStorage("shop");
    // if(that.data.NoMore==true)return;
    // if (app.functions.isEmpty(shop)) return;
    // 判断是否打烊
    // if (new Date((shop.sleepStartTime).replace(/-/g, '/')).getTime() < new Date().getTime() && new Date().getTime() < new Date((shop.sleepEndTime).replace(/-/g, '/')).getTime()) return;
    // let params = {
    //   centerId: shop.centreId,
    //   teamId: shop.id,
    //   mid: user.id,
    //   ismiaosha:1,
    //   offset: (that.data.currPage - 1) * that.data.perPage,
    //   limit: that.data.perPage,
    // }
    // app.api.products(params, (response) => {
    //   const { rows, total } = response.data;
    //   let items = app.functions.goods(rows);
    //   if (that.data.currPage == 1) {
    //     that.setData({
    //       loading: false
    //     })
    //     if (rows.length == 0) {
    //       that.setData({
    //         isempty: true
    //       })
    //     } else {
    //       that.setData({
    //         time:app.functions.OffGroup(),
    //         isempty: false,
    //         ProductsList: items
    //       })
    //     }
    //   } else {
    //     if(items.length==0){
    //       that.setData({
    //         NoMore:true,
    //         loading:false
    //       },()=>{
    //         // app.functions.showToast("触底了~");
    //       })
    //     }else{
    //       that.setData({
    //         ProductsList: that.data.ProductsList.concat(items)
    //       })
    //     }
    //   }
    // }, (e) => {
    //   app.functions.showToast(e.msg);
    // })
  }
})